// componentmanager.h : defines a class that manages and registers components, and provides a factory for their creation
//$Id: ComponentManager.h 16 2009-08-14 17:38:36Z brwarner2 $

#pragma once
#include "Component.h"

//Component Manager Class
class CComponentManager
{
public:
	CComponentManager(void);
	~CComponentManager(void);

	//Registers a component
	template<typename Component>
	void Register(void)
	{
		//Pass on functions to private-side
		Register( &Component::Factory, &Component::Destruct, &Component::RegisterGM, Component::COMPONENT_ID );
	}

	//Creates a component
	IComponent* Create( ComponentID );

	//Destroys a component
	void Destroy( IComponent*& , bool bClear = true);

	//Registers the GM functionality of all components
	void RegisterGM( gmMachine* );

private:
	//Function types
	typedef IComponent* (*Factory)(void);
	typedef void (*Destruct)(IComponent*);
	typedef void (*Reg)(gmMachine*);

	//Private registration function
	void Register( Factory, Destruct, Reg, ComponentID );

	//Registered component structure
	struct ComponentEntry
	{
		Factory create; //Factory function
		Destruct destroy; //Destruction function
		Reg reg; //GM Registration function
		ComponentID id; //Unique ID
	};

	//Type for list of registered components
	typedef std::map<ComponentID, ComponentEntry> ComponentDB;

	//List of registered components
	ComponentDB m_registeredComponents;

	//List of created components
	std::list<IComponent*> m_components;
};
